package com.netease.xone.itemview;

import protocol.meta.AppInfo;
import protocol.meta.AppInfoDetail;
import protocol.meta.AppVote;
import Utils.StackTraceUtil;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.netease.log.NTLog;
import com.netease.xone.R;


public class RenderInfoVote implements IRenderInfoBase, OnClickListener{

    private static final String TAG = RenderInfoVote.class.getSimpleName();

    private View mRoot;
    private TextView mCount;
    private LinearLayout mContainer;
    
    public RenderInfoVote(View v) {
        mRoot = v;
        mCount = (TextView)v.findViewById(R.id.vote_count);
        mContainer = (LinearLayout)v.findViewById(R.id.vote_container);
    }

    @Override
    public void renderView(Context context, AppInfo appInfo) {
        renderView(context, appInfo.getVote());
    }

    @Override
    public void renderView(Context context, AppInfoDetail appInfoDetail) {
    }
    
    public void renderView(Context context, AppVote vote) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        
        if (null == vote) {
            mRoot.setVisibility(View.GONE);
            return;
        }
        mRoot.setVisibility(View.VISIBLE);
        mCount.setText(String.valueOf(vote.getCount()));
        
        mContainer.removeAllViews();
        if (null != vote.getItemList() && vote.getItemList().size() > 0) {
            PKView pKView = new PKView(context, vote);
            
            // 设置item高度
            final int count = pKView.getChildCount();
            int itemHeight = 0;
            View child = null;
            if (vote.getFinished()) {
                itemHeight = context.getResources().getDimensionPixelSize(R.dimen.info_size_60);
            } else {
                itemHeight = context.getResources().getDimensionPixelSize(R.dimen.info_size_48);
            }
            for (int index = 0; index < count; index++) {
                child = pKView.getChildAt(index);
                if (child instanceof ImageView) {
                    continue;
                }
                ViewGroup.LayoutParams params = child.getLayoutParams();
                params.height = itemHeight;
                child.setLayoutParams(params);
            }
            
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            mContainer.addView(pKView, params);
            mContainer.setVisibility(View.VISIBLE);
        } else {
            mContainer.setVisibility(View.GONE);
            return;
        }
    }
    @Override
    public void onClick(View v) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        
    }
    
    public static class PKView extends AbsLinearLayout {
        // 分隔距离
        private int dividerHeight;
        private int mMaxWidth;
        private AppVote vote;
        
        public PKView(Context context, AppVote vote) {
            super(context);
            this.vote = vote;
            if (vote.getFinished()) {
                this.dividerHeight = context.getResources().getDimensionPixelSize(R.dimen.info_size_6);
            } else {
                this.dividerHeight = context.getResources().getDimensionPixelSize(R.dimen.info_size_20);
            }
            mMaxWidth = context.getResources().getDimensionPixelSize(R.dimen.info_size_435);
            setOrientation(VERTICAL);
            show();
        }

        @Override
        public int getDividerStyle() {
            return AbsLinearLayout.DIVIDER_MIDDLE;
        }

        @Override
        public View getDivider() {
            View v = null;
            v = new ImageView(getContext());
            v.setBackgroundDrawable(null);
            v.setPadding(0, dividerHeight, 0, 0);
            return v;
        }

        @Override
        public int getCount() {
            return vote.getItemList().size();
        }

        @Override
        public View getView(int position) {
            View child= null;
            TextView textView = null;
            if (vote.getFinished()) {
                // 投票结束模式
                child = View.inflate(getContext(), R.layout.item_view_info_vote_item1, null);
                if (vote.getCount() > 0) {
                    textView = (TextView)child.findViewById(R.id.vote_item_percent);
                    
                    textView.setText(String.valueOf(vote.getItemList().get(position).getCount()*100/vote.getCount()) + "%");
                    
                    ImageView image = (ImageView)child.findViewById(R.id.vote_item_bottom);
                    ViewGroup.LayoutParams params = image.getLayoutParams();
                    params.width = vote.getItemList().get(position).getCount()*mMaxWidth/vote.getCount();
                    image.setLayoutParams(params);
                }
            } else {
                // 投票模式
                child = View.inflate(getContext(), R.layout.item_view_info_vote_item, null);
            }
            textView = (TextView)child.findViewById(R.id.vote_item_index);
            textView.setText((position+1) + "、");
            
            textView = (TextView)child.findViewById(R.id.vote_item_title);
            textView.setText(vote.getItemList().get(position).getTitle());
            return child;
        }
    }
}
